package com.example.mtblog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mtblog.common.Result;
import com.example.mtblog.common.common;
import com.example.mtblog.dto.ArticleDTO;
import com.example.mtblog.dto.ArticleInfo;
import com.example.mtblog.dto.LoginUser;
import com.example.mtblog.dto.UserAuthority;
import com.example.mtblog.entity.Article;
import com.example.mtblog.mapper.ArticleMapper;
import com.example.mtblog.service.ArticleService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author MT
 * @since 2023-10-293
 */
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Resource
    ArticleService articleService;

    @Resource
    ArticleMapper articleMapper;





    /**
    * @description: 获取推荐文章
    * @author MT
    * @date 2023/10/23 11:15
     */
    @GetMapping("/getRecommendArticle")
    public Result<?> getRecommendArticle(
            @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
            HttpServletRequest request){

        Page<ArticleDTO> page = null;

        try {
            //获取用户ID,未登录时为 -1
            Integer uid = common.getLoginUid(request);
            page = articleService.getRecommendArtilcePage(pageNo,pageSize,uid);

        }catch (Exception e){
            String msg = "error in personal/get " + "\n"  +e ;
            log.error(msg);
            return Result.error(msg);
        }

        return Result.success(page);

    }

    /**
     * @description: 根据条件查询文章 -1
     * @author MT
     * @date 2023/10/23 11:15
     */
    @GetMapping("/search")
    public Result<?> searchArticle(HttpServletRequest request,
                                   @RequestParam(value = "articleId",defaultValue = "-1") Integer articleId){

        QueryWrapper<ArticleDTO> wrapper = new QueryWrapper<>();

        try {
            //获取用户ID,未登录时为 -1
            Integer uid = common.getLoginUid(request);

            if(!articleId.equals(-1))
                wrapper.eq("id",articleId);

//            articleService.


        }catch (Exception e){
            String msg = "error in personal/search " + "\n"  +e ;
            log.error(msg);
            return Result.error(msg);
        }

        return  Result.success();
    }


    /**
     * @description: 查看文章详情
     * @author MT
     * @date 2023/10/23 11:15
     */
    @GetMapping("/detail")
    public Result<?> selectArticleById(HttpServletRequest request,
                                      @RequestParam(value = "articleId",defaultValue = "-1") Integer articleId){

        QueryWrapper<ArticleDTO> wrapper = new QueryWrapper<>();

        try {
            //获取用户ID,未登录时为 -1
            Integer uid = common.getLoginUid(request);

            ArticleDTO articleDTO = articleMapper.selectArticleById(articleId);


            Result<?> res =  articleService.canViewArticle(articleDTO,uid);
            if(res.getCode() == 200)
                articleService.read(uid,articleId);

            return  res;

        }catch (Exception e){
            String msg = "error in personal/search " + "\n"  +e ;
            log.error(msg);
            return Result.error(msg);
        }


    }

    /**
     * @description: 新增文章
     * @author MT
     * @date 2023/10/23 11:15
     */
    @PostMapping("/add")
    public Result<?> addArticle(@RequestBody Article article, HttpServletRequest request){

        ArticleDTO articleDTO = null;

        String message ="发布成功,等待管理员审核";

        try {
            LoginUser loginUser = (LoginUser) request.getAttribute("loginUser");
            Integer author = loginUser.getUid();


            //不合法文章直接返回
            Result<?> res = articleService.judgeArticle(article);

            if(res.getCode() != 200)
                return res;

            //是否有发布权限
            Result<?> canUploadArticle = articleService.canUploadArticle(author);

            if(canUploadArticle.getCode()!=200)
                return canUploadArticle;

            //初始化文章
            article = articleService.initArticle(article,author);

            //插入数据库
            articleMapper.insert(article);


            ArticleInfo articleInfo = common.getArticleInfo(article.getId());
            articleInfo.setCreateBy(article.getCreateBy());
            common.setArticleInfo(articleInfo);

            //更新用户信息 今日|总共 上传文章数量 +1
            UserAuthority userAuthority = common.getUserAuthority(author);
            userAuthority.setUserDayPublishArticleNum(common.add(userAuthority.getUserDayPublishArticleNum(),1));
            userAuthority.setUserTotalPublishArticleNum(common.add(userAuthority.getUserTotalPublishArticleNum(),1));
            common.setUserAuthority(userAuthority);


            //返回新增文章
            articleDTO = articleMapper.selectArticleById(article.getId());


        }catch (Exception e){
            String msg = "error in article/add " + "\n" ;
            log.error(msg + e);
            return Result.error(msg);
        }
        return  Result.success(articleDTO,message);

    }


    /**
    * @description: 点赞收藏
    * @author MT
    * @date 2024/4/12 20:45
    */

    @GetMapping("/likeCollect")
    public Result<?> likeCollectArticle( HttpServletRequest request ,

                                         @RequestParam(value = "id",defaultValue = "0") Integer id,
                                         @RequestParam(value = "like",defaultValue = "false") Boolean like,
                                         @RequestParam(value = "collect",defaultValue = "false") Boolean collect){

        try {
            //获取用户ID,未登录时为 -1
            Integer uid = common.getLoginUid(request);

            if(id==0)
                return Result.error("ID不存在");
            if(like)
                return  articleService.likeArtilcle(uid,id);
            if(collect)
                return  articleService.collectArtilcle(uid,id);
        }catch (Exception e){
            String msg = "error in personal/search " + "\n"  +e ;
            log.error(msg);
            return Result.error(msg);
        }

        return Result.success();
    }



    /***获取作者发布的文章
    * @description: NULL
    * @author MT
    * @date 2024/4/13 22:26
    */
    @GetMapping("/getArticleByAuthor")
    public Result<?> getArticleByAuthor( HttpServletRequest request ,
                                         @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                         @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                                         @RequestParam(value = "author",defaultValue = "0") Integer author){

        try {
            //获取用户ID,未登录时为 -1
            Integer uid = common.getLoginUid(request);

            if(author==0)
                return Result.error("ID不存在");

            return articleService.getArticlePageByAuthor(pageNo,pageSize,author,uid);

        }catch (Exception e){
            String msg = "error in article/getArticleByAuthor " + "\n"  +e ;
            log.error(msg);
            return Result.error(msg);
        }

    }



    /***获取点赞收藏的文章
    * @description: NULL
    * @author MT
    * @date 2024/4/13 21:38
    */
    @GetMapping("/getArticleByLikeCollect")
    public Result<?> getArticleByLikeCollect(
            HttpServletRequest request ,
            @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
            @RequestParam(value = "author",defaultValue = "0") Integer author,
            @RequestParam(value = "like",defaultValue = "false") Boolean like,
            @RequestParam(value = "collect",defaultValue = "false") Boolean collect){
        try {
            //获取用户ID,未登录时为 -1
            Integer uid = common.getLoginUid(request);
            if(uid == -1)
                return  Result.noAuth("没有权限");
            if(author==0)
                return Result.error("ID不存在");
            return articleService.getArticlePageByLikeOrCollect(pageNo,pageSize,author,uid,like,collect);
        }catch (Exception e){
            String msg = "error in article/getArticleByLikeCollect " + "\n"  +e ;
            log.error(msg);
            return Result.error(msg);
        }
    }




}

